"""
Case Type   : Log_Max_Size
Case Name   : gs_guc reload方式设置log_max_size为无效值，合理报错
Create At   : 2024/10/24
Owner       : liu-tong-8848
Description :
              1.查看参数log_max_size默认值
              2.gs_guc reload方式设置log_max_size为小数
              3.gs_guc reload方式设置log_max_size为负数
              4.gs_guc reload方式设置log_max_size为字符串
              5.gs_guc reload方式设置log_max_size为bool
              6.gs_guc reload方式设置log_max_size为*%Y#-等特殊字符
              7.gs_guc reload方式设置log_max_size为大于边界值
              8.恢复log_max_size默认值
Expect      :
              1.默认值为0
              2.设置失败，合理报错
              3.设置失败，合理报错
              4.设置失败，合理报错
              5.设置失败，合理报错
              6.设置失败，合理报错
              7.设置失败，合理报错
              8.成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')

    def test_add_dimension(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = '--step1:show参数默认值;expect:参数默认值正常显示'
        self.log.info(step1)
        sql = "show log_max_size;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("0", result, "执行失败" + step1)
        self.assertNotIn("ERROR", result, "执行失败" + step1)

        step2 = '--step2:gs_guc reload方式设置log_max_size为小数;expect:设置失败，合理报错'
        self.log.info(step2)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=111.222")
        self.assertFalse(result, '执行失败:' + step2)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step2)

        step3 = '--step3:gs_guc reload方式设置log_max_size为负数;expect:设置失败，合理报错'
        self.log.info(step3)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=-12345")
        self.assertFalse(result, '执行失败:' + step3)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step3)

        step4 = '--step4:gs_guc reload方式设置log_max_size为字符串;expect:设置失败，合理报错'
        self.log.info(step4)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size='abcdefg'")
        self.assertFalse(result, '执行失败:' + step4)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step4)

        step5 = '--step5:gs_guc reload方式设置log_max_size为bool;expect:设置失败，合理报错'
        self.log.info(step5)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=FALSE")
        self.assertFalse(result, '执行失败:' + step5)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step5)

        step6 = '--step6:gs_guc reload方式设置log_max_size为*%Y#-等特殊字符;expect:设置失败，合理报错'
        self.log.info(step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=*")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=%")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=￥")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=$")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=#")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=-")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=@")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=&")
        self.assertFalse(result, '执行失败:' + step6)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step6)

        step7 = '--step7:gs_guc reload方式设置log_max_size为大于边界值;expect:设置失败，合理报错'
        self.log.info(step7)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=2147483648")
        self.assertFalse(result, '执行失败:' + step7)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step7)

        step8 = '--step8:恢复log_max_size默认值;expect:恢复成功'
        self.log.info(step8)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=0")
        self.assertTrue(result, '执行失败:' + step8)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step8)

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
